//
//  MovieContentView.m
//  Movie
//
//  Created by cjy on 16/3/23.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "MovieContentView.h"
#import "MovieModel.h"
#import <UIImageView+WebCache.h>

@implementation MovieContentView

- (instancetype)initWithFrame:(CGRect)frame Width:(CGFloat)width model:(MovieModel *)model collor:(UIColor *)collor{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
        
        
        
        _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
        
        [self addSubview:_imageView];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, [UIScreen mainScreen].bounds.size.width, 30)];
        _titleLabel.font = [UIFont boldSystemFontOfSize:16];
        _titleLabel.textColor = collor;
        [self addSubview:_titleLabel];
        
        _lineView = [[UIView alloc]initWithFrame:CGRectMake(5, 35, 240, 1)];
        _lineView.backgroundColor = collor;
        [self addSubview:_lineView];
        
        _littleLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 38, [UIScreen mainScreen].bounds.size.width, 20)];
        _littleLabel.textColor = collor;
        _littleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_littleLabel];
        
        _descripLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 50, [UIScreen mainScreen].bounds.size.width - 10, 90)];
        _descripLabel.font = [UIFont systemFontOfSize:13];
        _descripLabel.numberOfLines = 0;
        _descripLabel.textColor = collor;
        [self addSubview:_descripLabel];
        
        
        [_imageView sd_setImageWithURL:[NSURL URLWithString:model.coverForDetail]];
        
        [self setData:model];
    }
    return self;
}


- (void)setData:(MovieModel *)model {
    
    self.titleLabel.text = model.title;
    self.descripLabel.text = model.descrip;
    
    NSInteger time = model.duration ;
    NSString *timeString = [NSString stringWithFormat:@"%02ld'%02ld''",time/60,time% 60];//显示的是音乐的总时间
    NSString *string = [NSString stringWithFormat:@"#%@ / %@",model.category, timeString];
    self.littleLabel.text = string;
    
    
    __weak typeof(self) weakSelf = self;
    
    
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:model.coverBlurred] options:(SDWebImageRetryFailed) progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        
        if (image) {
            
            CABasicAnimation *contentsAnimation = [CABasicAnimation animationWithKeyPath:@"contents"];
            contentsAnimation.duration = 1.0f;
            contentsAnimation.fromValue = self.imageView.image ;
            contentsAnimation.toValue = image;
            
            contentsAnimation.removedOnCompletion = YES;
            contentsAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [weakSelf.imageView.layer addAnimation:contentsAnimation forKey:nil];
            
            weakSelf.imageView.image = image;
            
        }
        
    }];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
